package com.bestv.datalake.controller;

import com.bestv.datalake.exception.ExceptionResponse;
import com.bestv.datalake.exception.LogRuleConflictException;
import com.bestv.datalake.exception.LogRuleExistException;
import com.bestv.datalake.exception.LogRuleNotFoundException;
import com.bestv.datalake.model.LogHash;
import com.bestv.datalake.service.LogHashService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zg on 2016/6/27.
 */
@RestController
@RequestMapping("/datalake/v1/logHashs")
@Api(value = "LogHash", description = "提供LogHash相关CURD服务")
public class LogHashController {

    private Logger logger = Logger.getLogger(LogHashController.class);

    @Autowired
    private LogHashService logHashService;

//    @Autowired
//    private CityRepository cityRepository;


    @RequestMapping(value="",method= RequestMethod.GET)
    @ApiOperation(value="Get LogHashs", notes = "列出所有日志解析规则，支持主题过滤", produces="application/json", nickname = "getLogHashs")
    public List<LogHash> getLogHashBySubject(@RequestParam(value="subjectName",required = false) String subjectName) {
        if(subjectName!=null) {
            LogHash lp = logHashService.getLogHashBySubject(subjectName);
            if(lp==null){
                throw new LogRuleNotFoundException("当前主题对应的日志解析规则没有找到，主题=" + subjectName);
            }
            else {
                List<LogHash> list = new ArrayList<LogHash>();
                list.add(lp);
                return list;
            }
        }
        else{
            return logHashService.getAllLogHashs();
        }
    }

    @RequestMapping(value="/{logHashId}",method= RequestMethod.GET)
    @ApiOperation(value="Get LogHash", notes = "获得指定的日志解析规则", produces="application/json", nickname = "getLogHash")
    public LogHash getLogHash(@PathVariable Long logHashId){
        this.validateLogHashByID(logHashId);
        //return this.LogHashepository.findById(LogHashId).get();
        return logHashService.getLogHashByLogHashId(logHashId);
    }





    @RequestMapping(value="",method= RequestMethod.POST)
    @ResponseBody
    @ApiOperation(value="Create LogHash", notes = "新建日志解析规则", produces="application/json", nickname = "createLogHash")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "创建成功"),
            @ApiResponse(code = 409, message = "该主题对应的日志解析规则已经存在",response = ExceptionResponse.class)
    })
    public LogHash createLogHash(@RequestBody LogHash logHash){
        if(logHashService.getLogHashBySubject(logHash.getEnt_name())==null) {
            logHashService.createLogHash(logHash);
            return logHashService.getLogHashBySubject(logHash.getEnt_name());
        }
        else{
            throw new LogRuleExistException("该主题对应的日志解析规则已经存在");
        }
    }

    @RequestMapping(value="/{logHashId}",method= RequestMethod.PUT)
    @ResponseBody
    @ApiOperation(value="Update LogHash", notes = "更新日志解析规则，注意：日志解析规则ID必须一致", produces="application/json", nickname = "updateLogHash")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "更新成功"),
            @ApiResponse(code = 404, message = "日志解析规则不存在",response = ExceptionResponse.class),
            @ApiResponse(code = 409, message = "新的日志解析规则内的ID或主题和原来的日志解析规则不一致",response = ExceptionResponse.class)
    })
    public LogHash updateLogHash(@PathVariable Long logHashId, @RequestBody LogHash logHashNew){
        validateLogHashByID(logHashId);
        LogHash logHashOld = logHashService.getLogHashByLogHashId(logHashId);
        if(logHashOld.getEnt_name().equals(logHashNew.getEnt_name())&&logHashOld.getId().equals(logHashNew.getId())) {
            logHashService.updateLogHash(logHashNew);
            return logHashService.getLogHashByLogHashId(logHashId);
        }else {
            throw new LogRuleConflictException("新的日志解析规则内的ID或主题和原来的日志解析规则不一致");
        }
    }

    @RequestMapping(value="/{logHashId}",method= RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value="Delete LogHash By ID", notes = "基于日志解析规则ID删除对应解析规则", produces="application/json", nickname = "deleteLogHashByID")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "更新成功"),
            @ApiResponse(code = 404, message = "日志解析规则不存在",response = ExceptionResponse.class)
    })
    public String deleteLogHash(@PathVariable Long logHashId){
        validateLogHashByID(logHashId);
        logHashService.deleteLogHash(logHashId);
        return "delete succeed!";
    }

    @RequestMapping(value="",method= RequestMethod.DELETE)
    @ResponseBody
    @ApiOperation(value="Delete LogHash By SubjectName", notes = "基于主题名称删除对应解析规则", produces="application/json", nickname = "deleteLogHashBySubjectName")
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "更新成功"),
            @ApiResponse(code = 404, message = "日志解析规则不存在",response = ExceptionResponse.class)
    })
    public String deleteLogHash(@RequestParam(value="subjectName",required = true) String subjectName){
        validateLogHashBySubjectName(subjectName);
        LogHash LogHash = logHashService.getLogHashBySubject(subjectName);
        logHashService.deleteLogHash(LogHash);
        return "sucess";
    }

    private void validateLogHashByID(Long id) {
        if(logHashService.getLogHashByLogHashId(id)==null){
        throw new LogRuleNotFoundException("当前ID对应的日志解析规则没有找到, ID=" + id);
        }
    }

    private void validateLogHashBySubjectName(String subjectName) {
        if(logHashService.getLogHashBySubject(subjectName)==null) {
            throw new LogRuleNotFoundException("当前主题对应的日志解析规则没有找到，主题=" + subjectName);
        }
    }


}
